<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="title" :ok-text="okText" @ok="handleSubmit" width="750px">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="{ xs: { span: 24 }, sm: { span: 5 } }"
      :wrapperCol="{ xs: { span: 24 }, sm: { span: 16 } }">
      <a-row>
        <a-col :span="24">
          <a-form-item label="科研实验方案" v-bind="validateInfos.entrustId">
            <JDictSelectTag v-model:value="formData.entrustId" dictCode="lab_wscientific,plan_title,id"
              :show-choose-option="true" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="实验项目名称" v-bind="validateInfos.itemName">
            <a-input v-model:value="formData.itemName" :disabled="disabled"  />
            <!-- <a-button style="color:rgb(80, 173, 255);left: 10px;" @click="openForm()">引入实验模板</a-button> -->
          </a-form-item>
        </a-col>
        <!-- <a-col :span="24">
          <a-form-item label="指定模板" v-bind="validateInfos.entrustId">
            <JDictSelectTag v-model:value="formData.entrustId" dictCode="lab_wscientific,plan_title,id" :show-choose-option="true" />
          </a-form-item>
        </a-col> -->
        <a-col :span="24">
          <a-form-item label="实验人员" v-bind="validateInfos.userName">
            <!-- <a-input v-model:value="formData.userName" :disabled="disabled" /> -->
            <a-input v-model:value="formData.userName" size="large" class="input" @click="handleAuditModal2" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="复核人员" v-bind="validateInfos.auditName">
            <a-input v-model:value="formData.auditName" size="large" class="input" @click="handleAuditModal" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="备注" v-bind="validateInfos.remark">
            <a-input v-model:value="formData.remark" :disabled="disabled" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </BasicModal>
  <LabTemPOD @register="registerTenms" @getRatifyResultS2="onAuditOks1"></LabTemPOD>
  <TableUserModal @register="registerUserModal" @getAuditResult="onAuditOk" />
  <TableUserModal @register="registerUserModal2" @getAuditResult="onAuditOk2" />
</template>

<script lang="ts" setup>
import { BasicModal, useModalInner, useModal } from '/@/components/Modal';
import { ref, reactive } from 'vue';
import { Form } from 'ant-design-vue';
import LabTemPOD from './LabTemPOD.vue'
import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
import TableUserModal from './LabUserTableList.vue';
import { saveOrUpdate } from '../LabWscientificItem.api';
import { useUserStore } from '/@/store/modules/user';

const userStore = useUserStore();
const title = ref<string>('新增-科研项目');
//const okText = ref<string>('确认');
const okText = ref<string>('下一步');
const formRef = ref();

const disabled = ref(false);
let options = ref([]);
// const emit = defineEmits(['register']);reload
const emits = defineEmits(['success', 'mainTableReload']);
// const emits = defineEmits<{
//   (event: 'success', object: object): void;
// }>();
const useForm = Form.useForm;
const formData = reactive<Record<string, any>>({
  id: '',
  itemName: '',
  basisName: '',
  basisNum: '',
  basisScope: '',
  basisAtta: '',
  bigTypeName: '',
  midTypeName: '',
  team: '',
  userId: '',
  userName: '',
  auditName: '',
  auditId: '',
  itemStatus: '待分配',
  itemFinishTime: '',
  entrustType: '科研项目',
  entrustId: '',
  remark: '',
  tempId: '',
  tempName: '',
});

function reset() {
  formData.id = '';
  formData.itemName = '';
  formData.basisName = '';
  formData.basisNum = '';
  formData.basisScope = '';
  formData.basisAtta = '';
  formData.bigTypeName = '';
  formData.midTypeName = '';
  formData.team = '';
  formData.userId = '';
  formData.userName = '';
  formData.auditName = '';
  formData.auditId = '';
  formData.itemStatus = '待分配';
  formData.itemFinishTime = '';
  formData.entrustType = '科研项目';
  formData.entrustId = '';
  formData.remark = '';
  formData.tempId = '';
  formData.tempName = '';
}
const [registerUserModal, { openModal: openUModal }] = useModal();
const [registerUserModal2, { openModal: openUModal2 }] = useModal();
const [registerTenms, { openModal: openUModals1 }] = useModal();
function handleAuditModal() {
  openUModal(true, {
    oper: 'audit',
  });
}
//表单验证
const validatorRules = {
  itemName: [{ required: true, message: '请输入项目名称!' }],
  userName: [{ required: true, message: '请选择实验人员!' }],
  auditName: [{ required: true, message: '请选择复核人员!' }],
};
const { validate, validateInfos } = useForm(formData, validatorRules, { immediate: true });

const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  setModalProps({ confirmLoading: false });
  reset();

  if (data.bool) {
    title.value = '编辑 - 科研项目';
    //console.log('data.record', data.record);
    Object.assign(formData, data.record);
    disabled.value = true;
    okText.value = '下一步';
  } else {
    title.value = '新增-科研项目';
    disabled.value = false;
    okText.value = '下一步';
    formData.entrustId = '';
    formData.userId = userStore.getUserInfo.id;
    formData.userName = userStore.getUserInfo.realname;
  }
});

async function handleSubmit() {

  // await validate();
  // await saveOrUpdate(formData, false).then((res) => {
  //   Object.assign(formData, res.result);
  //   closeModal();
  //   emits('success', formData);
  // });
  if (disabled.value) {
    await validate();
    await saveOrUpdate(formData, true).then((res) => {
      closeModal();
      emits('mainTableReload');
    });
  } else {
    await validate();
    await saveOrUpdate(formData, false).then((res) => {
      Object.assign(formData, res.result);
      closeModal();
      emits('success', formData);
    });
  }
}
//打开选择模板
function openForm() {
  openUModals1(true, {
    oper: 'audit',
  });
}
//打开检测人员列表
function handleAuditModal2() {
  openUModal2(true, {
    oper: 'audit',
  });
}
// 选择检测人员
function onAuditOk2(uId, uName) {
  formData.userId = uId;
  formData.userName = uName;
}
// 选择复核人员
function onAuditOk(uId, uName) {
  formData.auditId = uId;
  formData.auditName = uName;
}
// 选择模板
function onAuditOks1(uId, uName) {
  formData.itemId = uId;
  //formData.tempName = uName;
  formData.itemName = uName;
}
</script>

<style>
/**隐藏样式-modal确定按钮 */
.jee-hidden {
  display: none !important;
}
</style>
